/*
  Part of the MasterTool3D project - http://code.google.com/p/master-tool-3d

  Copyright (c) 2011 Ingo Pueschl

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation, version 2.1.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package techjit.api.handlers;

import java.io.File;

import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PFont;
import processing.core.PGraphics;
import techjit.gfx.core.GFX_Edge;
import techjit.gfx.core.GFX_Scene;
import techjit.gfx.util.CoordinateSystemDrawer;
import techjit.util.LocalWorkspaceConstants;
import techjit.util.RuntimeParams;

public class ScreenDrawingHandler {

	private PGraphics g;
	private PFont font;

	public void draw(PApplet p, GFX_Scene scene) {
		if (RuntimeParams.SHOW__2D_OVERLAY) {
			int currentScreenX = scene
					.get_currentSceneElement_screenPositionX();
			int currentScreenY = scene
					.get_currentSceneElement_screenPositionY();

			g.beginDraw();
			g.background(129);
			// <>
			// gui.text("this text resides in a separate graphics object", 10,
			// 15);
			// gui.text("with independent drawing routines", 10, 25);
			// gui.rect(30, 300, 40, 40);
			// ==>
			// gui.strokeWeight(4);
			g.pushMatrix();
			g.pushStyle();
			{
				// gui.stroke(RP.COLOR__RED);
				// gui.line(0, 0, p.width, 0);
				// gui.line(0, p.height - 1, p.width, p.height - 1);
				//
				// gui.stroke(RP.COLOR__GREEN);
				// gui.line(0, 0, 0, p.height);
				// gui.line(p.width - 1, 0, p.width - 1, p.height);

				if (RuntimeParams.SHOW__EDGES) {
					g.stroke(RuntimeParams.COLOR__CYAN);
					for (GFX_Edge edge : scene.edges) {
						edge.draw_screenEdges(g);
					}

				} else {
					g.stroke(RuntimeParams.COLOR__BLUEISH_RED);
					g.line(currentScreenX, 0, currentScreenX, p.height);
					g.line(0, currentScreenY, p.width, currentScreenY);

					g.stroke(RuntimeParams.COLOR__GRAY);
					g.line(p.mouseX, 0, p.mouseX, p.height);
					g.line(0, p.mouseY, p.width, p.mouseY);

					g.stroke(RuntimeParams.COLOR__RED);
					g.fill(RuntimeParams.COLOR__RED);
					CoordinateSystemDrawer.drawAxis2D(g, 0, p.width);

					g.translate(0, p.height - 1);
					g.stroke(RuntimeParams.COLOR__RED);
					g.fill(RuntimeParams.COLOR__RED);
					CoordinateSystemDrawer.drawAxis2D(g, 0, p.width);
					g.translate(0, -(p.height - 1));

					g.rotate(PApplet.radians(90));
					g.stroke(RuntimeParams.COLOR__GREEN);
					g.fill(RuntimeParams.COLOR__GREEN);
					CoordinateSystemDrawer.drawAxis2D(g, 0, p.height);

					g.translate(0, -(p.width - 1));
					g.stroke(RuntimeParams.COLOR__GREEN);
					g.fill(RuntimeParams.COLOR__GREEN);
					CoordinateSystemDrawer.drawAxis2D(g, 0, p.height);
				}
			}
			g.popMatrix();
			g.popStyle();
			// </>
			// put arbitrary 2D-code here
			g.endDraw();

			g.loadPixels();
			p.loadPixels();
			for (int i = 0; i < p.pixels.length; i++)
				if (g.pixels[i] != p.color(129))
					p.pixels[i] = g.pixels[i];
			p.updatePixels();
			g.updatePixels();
		}

	}

	public void init(PApplet p) {
		g = p.createGraphics(p.width, p.height, PConstants.JAVA2D);
		File file = new File(LocalWorkspaceConstants.REPOSITORY_PATH
				+ "SoftwareVisualizer/data/" + "CharterBT-Roman-48.vlw");
		font = p.loadFont(file.getAbsolutePath());
		g.beginDraw();
		g.textFont(font);
		g.endDraw();
	}

}
